package com.gcsoftware.web.service;

import com.gcsoftware.authentications.beans.UserInfo;
import com.gcsoftware.authentications.helper.ShiroUtils;
import com.gcsoftware.configs.MailConfigs;
import com.gcsoftware.utils.GenerateIDs;
import com.gcsoftware.utils.SysConst;
import com.gcsoftware.utils.mail.MailUtils;
import com.gcsoftware.utils.mail.SendMailInfo;
import com.gcsoftware.web.beans.T_GW_VERIFY;
import com.gcsoftware.web.mapper.T_GW_VERIFYMAPPER;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by qs on 2018/3/27.
 */
@Service
public class T_GW_VERIFY_SERVICE {
    @Autowired
    private T_GW_VERIFYMAPPER mapper;

    @Autowired
    private MailConfigs mailConfigs;


    public int getMaxId() {
        String verifyId = mapper.getMaxId();
        if(StringUtils.isNotBlank(verifyId)) {
            return Integer.parseInt(verifyId.replace(GenerateIDs.ID_TYPE_VERIFY_ID,""));
        }
        return 0;
    }

    public String getVerifyId() {
        if ( 0 == GenerateIDs.getMaxVerifyId()) {
            GenerateIDs.setMaxVerifyId(getMaxId());
        }
        String verifyId =  GenerateIDs.GenerateId(GenerateIDs.ID_TYPE_VERIFY_ID);
        if(null != getById(verifyId)) {
            GenerateIDs.setMaxVerifyId(getMaxId());
            getVerifyId();
        }
        return verifyId;
    }


    public T_GW_VERIFY getById(String c_VERIFY_ID) {
        return mapper.getById(c_VERIFY_ID);
    }

    public List<T_GW_VERIFY> queryForList(String c_VERIFY_TYPE, String c_VERIFY_STATUS) {
        return mapper.queryForList(c_VERIFY_TYPE,c_VERIFY_STATUS);
    }

    public int updateVerifyStatus(String c_VERIFY_ID, String c_VERIFY_STATUS) {
        UserInfo loginedUser = ShiroUtils.getSessionAttribute("user");
        return mapper.updateVerify(loginedUser.getUserId(),c_VERIFY_STATUS,c_VERIFY_ID);
    }

    public int insertVerify(
            String c_SUBMIT_USER,
            String c_VERIFY_TYPE,
            String c_APP_ID,
            String c_INTERF_ID,
            String c_VERIFY_REMARK ,String name)
    {
        return insertVerify(c_SUBMIT_USER,getVerifyId(),c_VERIFY_TYPE,c_APP_ID,c_INTERF_ID,c_VERIFY_REMARK,name);
    }

    public int updateVerifyToUnable(String c_APP_ID, String c_INTERF_ID,String c_VERIFY_TYPE) {
        return mapper.unableVerify(c_APP_ID,c_INTERF_ID,c_VERIFY_TYPE);
    }

    public int insertVerify(
            String c_SUBMIT_USER,
            String c_VERIFY_ID,
            String c_VERIFY_TYPE,
            String c_APP_ID,
            String c_INTERF_ID,
            String c_VERIFY_REMARK ,String name)
    {
        T_GW_VERIFY verify = new T_GW_VERIFY();
        verify.setC_VERIFY_ID(c_VERIFY_ID);
        verify.setC_VERIFY_STATUS(SysConst.VERIFY_STATUS_UNVERIFY);
        verify.setC_SUBMIT_USER(c_SUBMIT_USER);
        verify.setC_VERIFY_TYPE(c_VERIFY_TYPE);
        verify.setC_APP_ID(c_APP_ID);
        verify.setC_INTERF_ID(c_INTERF_ID);
        verify.setC_VERIFY_REMARK(c_VERIFY_REMARK);
        if (1 ==mapper.insertVerify(verify)) {
            try {
                MailUtils.sendMail(getMailInfo(c_VERIFY_TYPE , name));
            } catch (Exception e) {
                e.printStackTrace();
            }
            return 1;
        }
        return 0;
    }

    private SendMailInfo getMailInfo(String c_VERIFY_TYPE,String name) {
        SendMailInfo info = new SendMailInfo();
        info.setDeliver(mailConfigs.getMailSendFrom());
        info.setReceiver(mailConfigs.getAdminMail());
        info.setHtml(true);

        switch (c_VERIFY_TYPE) {
            case SysConst.VERIFY_TYPE_USER_REGISTER:
                info.setSubject(mailConfigs.getSubjectAccountReg());
                info.setContent("有新用户注册了，请尽快审核！<br/>----------------------------<br/>&nbsp;&nbsp;&nbsp;&nbsp;账户名：" + name + "<br/>----------------------------<br/>");
                break;
            case  SysConst.VERIFY_TYPE_COMPANY_REGISTER:
                info.setSubject(mailConfigs.getSubjectCompanyReg());
                info.setContent("有新厂家注册了，请尽快审核！<br/>----------------------------<br/>&nbsp;&nbsp;&nbsp;&nbsp;公司名称：" + name + "<br/>----------------------------<br/>");
                break;
            case SysConst.VERIFY_TYPE_APP_REGISTER:
                info.setSubject(mailConfigs.getSubjectAppReg());
                info.setContent("有新应用注册了，请尽快审核！<br/>----------------------------<br/>&nbsp;&nbsp;&nbsp;&nbsp;应用名称：" + name + "<br/>----------------------------<br/>");
                break;
            case SysConst.VERIFY_TYPE_APP_CHANGED:
                info.setSubject(mailConfigs.getSubjectAppChange());
                info.setContent("有应用提交了变更申请，请尽快审核！<br/>----------------------------<br/>&nbsp;&nbsp;&nbsp;&nbsp;应用名称：" + name + "<br/>----------------------------<br/>");
                break;
            case SysConst.VERIFY_TYPE_APP_OFFLINE:
                info.setSubject(mailConfigs.getSubjectAppOffLine());
                info.setContent("有应用提交了下线申请，请尽快审核！<br/>----------------------------<br/>&nbsp;&nbsp;&nbsp;&nbsp;应用名称：" + name + "<br/>----------------------------<br/>");
                break;
            case SysConst.VERIFY_TYPE_INTERF_REGISTER:
                info.setSubject(mailConfigs.getSubjectInterfReg());
                info.setContent("有接口提交了注册申请，请尽快审核！<br/>----------------------------<br/>&nbsp;&nbsp;&nbsp;&nbsp;接口名称：" + name + "<br/>----------------------------<br/>");
                break;
            case SysConst.VERIFY_TYPE_INTERF_CHANGED:
                info.setSubject(mailConfigs.getSubjectInterfChange());
                info.setContent("有接口提交了变更申请，请尽快审核！<br/>----------------------------<br/>&nbsp;&nbsp;&nbsp;&nbsp;接口名称：" + name + "<br/>----------------------------<br/>");
                break;
            case SysConst.VERIFY_TYPE_INTERF_OFFLINE:
                info.setSubject(mailConfigs.getSubjectInterfOffLine());
                info.setContent("有接口提交了下线申请，请尽快审核！<br/>----------------------------<br/>&nbsp;&nbsp;&nbsp;&nbsp;接口名称：" + name + "<br/>----------------------------<br/>");
                break;
        }
        return info;
    }
}
